package com.otod.pm.prj.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.cd.doc.constants.ShareConstant;
import com.otod.cd.doc.mapper.CdDocFolderMapper;
import com.otod.cd.doc.service.CdDocFileService;
import com.otod.cd.doc.vo.CdDocFolderVo;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.DateUtils;
import com.otod.commons.utils.StringUtils;
import com.otod.pm.constants.PmConstant;
import com.otod.pm.prj.mapper.PmStepDocCfgMapper;
import com.otod.pm.prj.vo.PmStepDocCfgVo;
import com.otod.pm.prj.vo.StepVo;

/**
 * 
 * @author long
 *
 */
@Service
@Transactional(readOnly=true)
public class PmStepDocCfgService extends BaseService {
	@Autowired
	PmStepDocCfgMapper pmStepDocCfgMapper;
	@Autowired
	private TaskService taskService;
	@Autowired
	private StepService stepService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private CdDocFileService cdDocFileService;
	@Autowired
	private CdDocFolderMapper cdDocFolderMapper;
	/**
	 * 通过uuid查询数据
	 * @param uuid
	 * @return
	 */
	public PmStepDocCfgVo selectStepFoler(String uuid){
		PmStepDocCfgVo cfgVo = new PmStepDocCfgVo();
		cfgVo =  pmStepDocCfgMapper.getById(uuid);
		return cfgVo;
	}
	
	
	/**
	 * 插入数据 文件 或者文件夹得数据
	 * @param pmStepDocCfgVo
	 * @return
	 */
	public PmStepDocCfgVo insert(PmStepDocCfgVo pmStepDocCfgVo){
			pmStepDocCfgMapper.insert(pmStepDocCfgVo);
			return pmStepDocCfgVo;
	}
	
	
	/**
	 * 添加文件夹
	 * @param parentId 上级目录id
	 * @param stepId 项目id
	 * @return
	 */
	@Transactional
	public PmStepDocCfgVo addFoler(String parentId, String stepId){
		PmStepDocCfgVo cfgVo = new PmStepDocCfgVo();
		cfgVo.setParentId(parentId);
		cfgVo.setStepId(stepId);
		return insert(cfgVo);
	}
	
	
	/**
	 * 添加阶段文件夹
	 * @param uuid
	 * @param folderName
	 * @return
	 * 
	 * 
	 * 
	 */
	@Transactional
	public PmStepDocCfgVo addStepFoler(String uuid,String folderName,String empNo){
		PmStepDocCfgVo cfgVo = new PmStepDocCfgVo();
		cfgVo.setUuid(uuid);
		cfgVo.setFolderName(folderName);
		
		cfgVo.setStepId("");
		cfgVo.setFolderType(CommonConstant.FOLDER);
		cfgVo.setCreateEmpNo(empNo);
		cfgVo = insert(cfgVo);
		return cfgVo;
	}
	
	/**
	 * 添加文件或者文件夹
	 * @param uuid
	 * @param stepId
	 * @param folderType
	 * @param folderName
	 * @param parentId
	 */
	@Transactional
	public PmStepDocCfgVo addFolderOrFile(String uuid,String stepId,String folderType,String folderName,String parentId,String empNo){
		
		PmStepDocCfgVo cfgVo = new PmStepDocCfgVo();
		cfgVo.setUuid(uuid);
		cfgVo.setStepId(stepId);
		if(StringUtils.isEmpty(folderType)){
			cfgVo.setFolderType(CommonConstant.FOLDER);
		}else{
			cfgVo.setFolderType(folderType);
		}
	
		cfgVo.setFolderName(folderName);
		cfgVo.setParentId(stepId);
		if(StringUtils.isNotEmpty(parentId)){
			cfgVo.setParentId(parentId);
		}
		
		if(folderType==CommonConstant.FILE||CommonConstant.FILE.equals(folderType)){
			cfgVo.setRelatedId(uuid);
		}
		cfgVo.setCreateEmpNo(empNo);
		cfgVo = insert(cfgVo);
		return cfgVo;
	}
	
	/**
	 * 修改文件或者文件夹
	 * @param uuid
	 * @param stepId
	 * @param folderType
	 * @param folderName
	 * @param parentId
	 * @throws AppException 
	 */
	@Transactional
	public void updateaddFolderOrFile(String uuid,String stepId,String folderType,String folderName,String parentId){
		
		
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("uuid", uuid);
		//map.put("stepId", stepId);
		//map.put("folderType", folderType);
		map.put("folderName", folderName);
		if(StringUtils.isEmpty(parentId)){
			map.put("parentId", stepId);
		}else{
			map.put("parentId", parentId);
		}
		pmStepDocCfgMapper.update(map);
	}
	
	
	/**
	 * 通过阶段id查询数据
	 * @param stepId
	 * @return
	 */
	public List<PmStepDocCfgVo> queryForList(String stepId){
		List<PmStepDocCfgVo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("stepId", stepId);
		list = pmStepDocCfgMapper.queryForList(map);
		return list;
	}
	
	/**
	 * 通过uuid删除数据
	 * @param uuid
	 */
	@Transactional
	public void deleteById(String deleteUuid){
		String[] splitUuid = deleteUuid.split(",");
		for (String uuid : splitUuid) {
			pmStepDocCfgMapper.deleteById(uuid);
		}
	}
	
	/**
	 * 查询是否有重复的名字
	 * @param paretId  上级目录的id
	 * @param foderName  文件夹得名字
	 * @return
	 * @throws AppException 
	 */
	public PmStepDocCfgVo selectRepetitionFolderName(String folderName,String parentId,String stepId){
		PmStepDocCfgVo pmStepDocCfgVo = null;
		List<PmStepDocCfgVo> cfgVos = new ArrayList<PmStepDocCfgVo>();
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("folderName",folderName);
		map.put("paretId", parentId);
		map.put("stepId",stepId);
		
		cfgVos = pmStepDocCfgMapper.queryForList(map);
		if(Collections3.isNotEmpty(cfgVos)){
			pmStepDocCfgVo = cfgVos.get(0);
		}else{
			pmStepDocCfgVo= new PmStepDocCfgVo();
		}
		
		return pmStepDocCfgVo;
	}
	
	
	/****************************************项目文库*************************************/
	/**
	 * 添加项目文库
	 * @param prjId
	 * @throws AppException 
	 */
	@Transactional
	public void addProjectDoc(String prjId,String stepId) throws AppException{
		//查询此阶段 完成 之后 是否
		//删除文库表中uuid parentId包含 prjId的所有数据在添加
		StepVo stepVo = stepService.getById(stepId);
		if (StringUtils.equals(PmConstant.STEP_STATUS_FLISH, stepVo.getStatus()) && stepVo.getFilingFile()) {
			throw new AppException("阶段完成并已经归档不能在进行此种操作");
		}
		
		if (StringUtils.equals(PmConstant.STEP_STATUS_FLISH, stepVo.getStatus())){
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("filingFile", true);
			params.put("uuid", stepId);
			stepService.update(params);
		}
		
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		//首先判断是否在文库中存在
		//通过项目id 查询相关的项目信息
		PmStepDocCfgVo docCfgVo = pmStepDocCfgMapper.getById(prjId);
		CdDocFolderVo docFolderVo = cdDocFileService.getById(prjId);
		if (null == docFolderVo) {
			//1 首先添加项目信息到文库
			CdDocFolderVo cdDocFolderVo = new CdDocFolderVo();
			cdDocFolderVo.setUuid(docCfgVo.getUuid());
			cdDocFolderVo.setFolderType(docCfgVo.getFolderType());
			cdDocFolderVo.setFolderName(docCfgVo.getFolderName());

			cdDocFolderVo.setCreateEmp(sessionUser.getUserName());
			cdDocFolderVo.setCreateEmpNo(sessionUser.getUserNo());
			cdDocFolderVo.setCreateDt(DateUtils.getCurrentDate());
			cdDocFolderVo.setIsDel(CommonConstant.COMMON_NO);
			cdDocFolderVo.setType(ShareConstant.File_TYPE_PROJECT);
			cdDocFileService.insert(cdDocFolderVo);
		}
		
		deleteProjectDoc(stepId);		
		//2 首先查出阶段的上级目录是 prjId
		Map<String, Object> map = new HashMap<>();
		map.put("uuid", stepId);
		List<PmStepDocCfgVo> docCfgVos = pmStepDocCfgMapper.queryForList(map);
		
		for (PmStepDocCfgVo pmStepDocCfgVo : docCfgVos) {
			CdDocFolderVo cdDocFolderVo = new CdDocFolderVo();
			cdDocFolderVo.setUuid(pmStepDocCfgVo.getUuid());
			cdDocFolderVo.setFolderType(pmStepDocCfgVo.getFolderType());
			cdDocFolderVo.setFolderName(pmStepDocCfgVo.getFolderName());			
			cdDocFolderVo.setCreateEmp(sessionUser.getUserName());
			cdDocFolderVo.setCreateEmpNo(sessionUser.getUserNo());
			cdDocFolderVo.setCreateDt(DateUtils.getCurrentDate());
			cdDocFolderVo.setIsDel(CommonConstant.COMMON_NO);
			cdDocFolderVo.setType(ShareConstant.File_TYPE_PROJECT);
			//设置上级目录
			cdDocFolderVo.setParentId(prjId);
			 //全路径ID（逗号分隔）
			cdDocFolderVo.setFolderPath(prjId);
			 //全路径名称（逗号分隔）
			cdDocFolderVo.setFolderPathName(docCfgVo.getFolderName());
			cdDocFolderVo=cdDocFileService.insert(cdDocFolderVo);
			packagingProjectData(cdDocFolderVo);
		}
	}
	
	/**
	 * 把项目封装成folder 或 file 数据
	 */
	public void packagingProjectData(CdDocFolderVo folderVo){
		//查询表pm_step_Doc_Cfg    parentId 为uuid的数据
		List<PmStepDocCfgVo> pmStepDocCfgVos = Lists.newArrayList();
		
		pmStepDocCfgVos = selectParentId(folderVo.getUuid());
		CdDocFolderVo cdDocFolderVo2 = null;
		CdDocFolderVo cdDocFolderVo = null;
		for (PmStepDocCfgVo pmStepDocCfgVo : pmStepDocCfgVos) {
			cdDocFolderVo = new CdDocFolderVo();
			cdDocFolderVo.setUuid(pmStepDocCfgVo.getUuid());
			cdDocFolderVo.setFolderType(pmStepDocCfgVo.getFolderType());
			cdDocFolderVo.setFolderName(pmStepDocCfgVo.getFolderName());
			SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
			cdDocFolderVo.setCreateEmp(sessionUser.getUserName());
			cdDocFolderVo.setCreateEmpNo(sessionUser.getUserNo());
			cdDocFolderVo.setCreateDt(DateUtils.getCurrentDate());
			cdDocFolderVo.setIsDel(CommonConstant.COMMON_NO);
			cdDocFolderVo.setType(ShareConstant.File_TYPE_PROJECT);
			//设置上级目录
			cdDocFolderVo.setParentId(folderVo.getUuid());
			 //全路径ID（逗号分隔）
			cdDocFolderVo.setFolderPath(folderVo.getFolderPath()+","+folderVo.getUuid());
			 //全路径名称（逗号分隔）
			cdDocFolderVo.setFolderPathName(folderVo.getFolderPathName()+","+folderVo.getFolderName());
			//判断是不是文件

			if("40010002".equals(pmStepDocCfgVo.getFolderType())){
				cdDocFolderVo.setRelatedId(pmStepDocCfgVo.getRelatedId());
			}
			cdDocFolderVo2=cdDocFileService.insert(cdDocFolderVo);
			packagingProjectData(cdDocFolderVo2);
		}
		
	}
	
	/**
	 * 查询上级目录是uuid的
	 * @param parentId
	 * @return
	 */
	public List<PmStepDocCfgVo> selectParentId(String parentId){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("parentId", parentId);
		List<PmStepDocCfgVo> cfgVos = pmStepDocCfgMapper.queryForList(map);
		return cfgVos;
	}
	
	/**
	 * 通过项目的uuid删除数据
	 */
	@Transactional
	public void deleteProjectDoc(String uuid){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("uuid", uuid);
		map.put("folder_path", uuid);
		map.put("type", ShareConstant.File_TYPE_PROJECT);
		
		cdDocFileService.deleteProjectDoc(map);
	}
	
	/**
	 * 修改文件名字  和阶段名字
	 * @param uuid
	 * @param folderName
	 * @throws AppException 
	 */
	@Transactional
	public void update(String uuid,String folderName,String stepId) throws AppException{
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("folderName", folderName);
		map.put("stepId", stepId);
		//首先查询修改的名字是否重复
		List<PmStepDocCfgVo> cfgVos = pmStepDocCfgMapper.queryForList(map);
		
		if(Collections3.isNotEmpty(cfgVos)){
			throw new AppException("名字重复请重新修改");
		}else{
			map.put("uuid", uuid);		
			map.remove("stepId");
			pmStepDocCfgMapper.update(map);
		}
	}
}
